package com.chj.comp.common.auth.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.chj.comp.common.auth.config.SecurityConfig;
import com.chj.comp.common.auth.security.service.SecurityUserDetailService;
import com.chj.comp.common.auth.service.IAuthTokenService;
import com.chj.comp.common.auth.vo.AuthTokenVO;
import com.chj.comp.common.core.util.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @author ：cuihui
 * @date ：Created in 2022/2/21
 * @description：token接口服务类
 * @modified By：
 */
@Service
@Slf4j
public class AuthTokenService implements IAuthTokenService {

    @Autowired
    private SecurityUserDetailService securityUserDetailService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * Security配置文件
     */
    @Autowired
    private SecurityConfig securityConfig;

    @Override
    public AuthTokenVO getTokenByPassword(String username, String password) throws Exception{
        //密码需要客户端加密后传递
        UserDetails userDetails = securityUserDetailService.loadUserByUsername(username);
        if(userDetails == null){
            throw new Exception("用户不存在");
        }
        if(!passwordEncoder.matches(password,userDetails.getPassword())){
            throw new Exception("密码不正确");
        }
        if(!userDetails.isEnabled()){
            throw new Exception("帐号已被禁用");
        }
        HashMap param = new HashMap();
        param.put("client_id", securityConfig.getClientId());
        param.put("client_secret", securityConfig.getClientSecret());
        param.put("grant_type", "password");
        param.put("username", username);
        param.put("password", password);
        String response = HttpUtil.sendPost(securityConfig.getGetToken(), param);
        AuthTokenVO authTokenVO = JSONObject.parseObject(response, AuthTokenVO.class);
        return authTokenVO;
    }

    @Override
    public AuthTokenVO getTokenByClient(String clientId, String clientSecret) throws Exception {
        if(StringUtils.isEmpty(clientId)){
            throw new Exception("client_id不存在");
        }
        if(StringUtils.isEmpty(clientSecret)){
            throw new Exception("client_secret不存在");
        }
        HashMap param = new HashMap();
        param.put("client_id", clientId);
        param.put("client_secret", clientSecret);
        param.put("grant_type", "client_credentials");
        String response = HttpUtil.sendPost(securityConfig.getGetToken(), param);
        AuthTokenVO authTokenVO = JSONObject.parseObject(response, AuthTokenVO.class);
        return authTokenVO;
    }

    @Override
    public AuthTokenVO getTokenBySms(String phone, String code) throws Exception {
        if(StringUtils.isEmpty(phone)){
            throw new Exception("phone不存在");
        }
        if(StringUtils.isEmpty(code)){
            throw new Exception("code不存在");
        }
        HashMap param = new HashMap();
        param.put("client_id", securityConfig.getClientId());
        param.put("client_secret", securityConfig.getClientSecret());
        param.put("grant_type", "sms_code");
        param.put("phone", phone);
        param.put("code", code);
        String response = HttpUtil.sendPost(securityConfig.getGetToken(), param);
        AuthTokenVO authTokenVO = JSONObject.parseObject(response, AuthTokenVO.class);
        return authTokenVO;
    }

    @Override
    public AuthTokenVO getTokenByCode(String code, String redirectUri) {
        HashMap param = new HashMap();
        param.put("client_id", securityConfig.getClientId());
        param.put("client_secret", securityConfig.getClientSecret());
        param.put("grant_type","authorization_code");
        param.put("code",code);
        param.put("redirect_uri",redirectUri);
        String response = HttpUtil.sendPost(securityConfig.getGetToken(), param);
        AuthTokenVO authTokenVO = JSONObject.parseObject(response, AuthTokenVO.class);
        return authTokenVO;
    }

    @Override
    public AuthTokenVO refreshToken(String refresh_token) {
        HashMap param = new HashMap();
        param.put("client_id", securityConfig.getClientId());
        param.put("client_secret", securityConfig.getClientSecret());
        param.put("grant_type","refresh_token");
        param.put("refresh_token",refresh_token);
        String response = HttpUtil.sendPost(securityConfig.getGetToken(), param);
        AuthTokenVO authTokenVO = JSONObject.parseObject(response, AuthTokenVO.class);
        return authTokenVO;
    }
}
